package com.gohome.gohomechat.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.easemob.chat.EMGroup;
import com.easemob.chat.EMMessage;
import com.gohome.gohomechat.GoHomeHelper;
import com.gohome.gohomechat.R;
import com.gohome.gohomechat.model.entify.ChatType;
import com.gohome.gohomechat.model.entify.Contant;
import com.gohome.gohomechat.model.entify.Friend;
import com.gohome.gohomechat.model.entify.Group;
import com.gohome.gohomechat.presenter.AddressPresenter;
import com.gohome.gohomechat.presenter.GroupChatPresenter;
import com.gohome.gohomechat.presenter.presenterlistener.AddressPresenterInterface;
import com.gohome.gohomechat.presenter.presenterlistener.GroupChatPresenterInterface;
import com.gohome.gohomechat.ui.adapter.AddressListViewAdapter;
import com.gohome.gohomechat.ui.adapter.GroupChatListViewAdapter;
import com.gohome.gohomechat.ui.fragment.BaseFragment;
import com.gohome.gohomechat.ui.viewlistener.AddressViewInterface;
import com.gohome.gohomechat.ui.viewlistener.GroupChatViewInterface;
import com.gohome.gohomechat.utils.ToActivityUtil;
import com.gohome.gohomechat.utils.ToastUtil;
import com.gohome.gohomechat.view.QuickIndexView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/3/1.
 */
public class GroupChatActivity extends BaseActivity implements QuickIndexView.OnIndexChangedListener, GroupChatViewInterface, AdapterView.OnItemClickListener, View.OnClickListener {
    private ImageButton groupchat_imgbtn_back;
    private GroupChatListViewAdapter adapter;
    private ListView listView;
    private QuickIndexView quickIndexView;
    private TextView groupchat_word;
    private List<Group> datas;
    private GroupChatPresenterInterface groupChatPresenterInterface;
    private String[] actions = {Contant.ACTION_CONTACT_CHANAGED,
            Contant.ACTION_CONTACT_REFUSED, Contant.NEW_FRIENDS_INVITATION, Contant.NEW_FRIENDS_ADD, Contant.ACCOUNT_REMOVED};

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    //隐藏word
                    groupchat_word.setVisibility(View.GONE);
                    break;
            }

            return false;
        }
    });


    @Override
    protected int getLayout() {
        return R.layout.activity_groupchat;
    }

    @Override
    protected void initPresenter() {
        this.groupChatPresenterInterface = new GroupChatPresenter(this);
    }

    @Override
    protected void initView() {
        groupchat_imgbtn_back=findView(R.id.groupchat_imgbtn_back);
        listView = findView(R.id.groupchat_listview);
        quickIndexView = findView(R.id.groupchat_indexview);
        adapter = new GroupChatListViewAdapter(this);
        groupchat_word = findView(R.id.groupchat_word);
        datas=new ArrayList<>();
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
        for (int i = 0; i < actions.length; i++) {
            IntentFilter intentFilter = new IntentFilter(actions[i]);
            localBroadcastManager.registerReceiver(broadcastReceiver, intentFilter);
        }
    }

    @Override
    protected void initData() {

        View headerVeiw_create = LayoutInflater.from(this).inflate(R.layout.groupchat_listview_header_create, null);
        View headerVeiw_public = LayoutInflater.from(this).inflate(R.layout.groupchat_listview_header_public, null);

        listView.addHeaderView(headerVeiw_create);
        listView.addHeaderView(headerVeiw_public);

        listView.setOnItemClickListener(this);
        groupchat_imgbtn_back.setOnClickListener(this);

    }

    @Override
    protected void onStart() {
        groupChatPresenterInterface.refreshData();
        quickIndexView.setOnIndexChangedListener(this);
        listView.setAdapter(adapter);
        super.onStart();
    }

    @Override
    public void onIndexChanged(String word) {
        groupchat_word.setText(word);
        groupchat_word.setVisibility(View.VISIBLE);
        //移除未处理的消息
        handler.removeCallbacksAndMessages(null);
        //发延迟消息
        handler.sendEmptyMessageDelayed(100, 1500);

        //滑动listview
        //查找对应的item
        for (int i = 0; i < datas.size(); i++) {
            String fWord = datas.get(i).getPinyin().substring(0, 1).toUpperCase();
            if (word.equals(fWord)) {
                listView.setSelection(i);
                return;
            }
        }
    }

    @Override
    public void onUp() {

    }


    @Override
    public void refreshSuccess(List<Group> datas) {
        Log.i("wcl", datas+",.,.,.");
        this.datas = datas;
        adapter.addData(datas);
    }

    @Override
    public void refreshFailed() {
        ToastUtil.showToast("群组列表获取失败");
    }

    @Override
    public void showDialog() {
        showProgressDialog();
    }

    @Override
    public void dismisssDialog() {
        dismissProgressDialog();
    }


    public void toNoticeActivitySuccess(Bundle bundle) {

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                ToActivityUtil.startToActivity(this,CreateGroupActivity.class);
                break;
            case 1:
                break;
            default:
                Bundle bundle = new Bundle();
                bundle.putParcelable("content",new ChatType(EMMessage.ChatType.GroupChat,datas.get(position - 2).getGroupId()));
//                bundle.putString("content", datas.get(position - 2).getGroupId());
//                bundle.putString("chattype", EMMessage.ChatType.GroupChat);
                ToActivityUtil.startToActivity(this, ChatActivity.class, bundle);
//                GoHomeHelper.getInstance().readMessage(datas.get(position - 2).getUserName());
                break;
        }
    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {


        }
    };

    @Override
    public void onResume() {

        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.groupchat_imgbtn_back:
                finish();
                break;
        }
    }
}
